package sendmail;

import java.io.IOException;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.google.appengine.api.datastore.DatastoreService;
import com.google.appengine.api.datastore.DatastoreServiceFactory;
import com.google.appengine.api.datastore.Entity;
import com.google.appengine.api.datastore.FetchOptions;
import com.google.appengine.api.datastore.Key;
import com.google.appengine.api.datastore.KeyFactory;
import com.google.appengine.api.datastore.Query;

public class RemoveEmail extends HttpServlet {
	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
		String email = req.getParameter(Constants.EMAIL);
		if (email == null || email.length() == 0)
			return;
		String lang = Constants.LANG_ENGLISH;
		if (Constants.LANG_KOREAN.equalsIgnoreCase(req
				.getParameter(Constants.LANG))) {
			lang = Constants.LANG_KOREAN;
		}
		DatastoreService dataStore = DatastoreServiceFactory
				.getDatastoreService();
		Key langKey = KeyFactory.createKey(Constants.LANG, lang);
		Query query = new Query(Constants.EMAIL, langKey);
		List<Entity> emails = dataStore.prepare(query).asList(
				FetchOptions.Builder.withDefaults());
		for (Entity entity : emails) {
			if (email.equals(entity.getProperty(Constants.EMAIL))) {
				dataStore.delete(entity.getKey());
			}
		}
		resp.getWriter().write("Successfuly Removed");
	}
}
